Opening a Port
Opening a Port
To open a port and associate a name with it, use the PPCOpen function. The
following program illustrates how you use the PPCOpen function to open a
port. In this listing, the name is "Inside Macintosh" and the port type string is
"Example". The location name is PPC. For
example:@.
// Opening a PPC port
// Assuming inclusion of MacHeaders
#include <PPCToolBox.h>
#include <Script.h>
#include
// Prototype your function like this prior to calling it
OSErr MyPPCOpen(PPCPortRefNum *,Boolean *);
OSErr MyPPCOpen(PPCPortRefNum *thePortRefNum, Boolean
*nbpRegisterFlag)
{
PPCOpenPBRec thePPCOpenPBRec;
PPCPortRec thePPCPortRec;
LocationNameRec theLocationNameRec;
OSErr err;
// nameScript and name should be resources to allow easy localization
thePPCPortRec.nameScript = smRoman; // Roman script
// name is a pascal style string, need to cast it to call strcpy
strcpy((char *)thePPCPortRec.name,"Inside Macintosh");
// convert back to Pascal string
CtoPstr(thePPCPortRec.name);
// the port type should always be hard-coded to allow the
// application to find ports of a particular type even after
// the name is localized
thePPCPortRec.portKindSelector = ppcByString;
// name is a pascal style string, need to cast it to call strcpy
strcpy((char *)thePPCPortRec.u.portTypeStr,"Example");
CtoPstr(thePPCPortRec.u.portTypeStr);
theLocationNameRec. locationKindSelector = ppcNBPTypeLocation;
// nbpType is a pascal style string, need to cast it to call strcpy
strcpy((char *) theLocationNameRec.u.nbpType,"PPC Example");
CtoPstr( theLocationNameRec.u.nbpType);
thePPCOpenPBRec.serviceType = ppcServiceRealTime;
thePPCOpenPBRec.resFlag = 0; // must be 0 for 7.0+
thePPCOpenPBRec.portName = &thePPCPortRec;
thePPCOpenPBRec. locationName = & theLocationNameRec;
// make this a visible entity on the network
thePPCOpenPBRec. networkVisible = TRUE;
err = PPCOpen(&thePPCOpenPBRec, FALSE); // synchronous
*thePortRefNum = thePPCOpenPBRec.portRefNum;
*nbpRegisterFlag = thePPCOpenPBRec.nbpRegistered;
return err;
}
The PPCOpen function opens a port with the port name and location name
specified in the name and location fields of the parameter block. When the
PPCOpen function completes execution, the portRefNum field returns the
port reference number. You can use the port reference number in the
to refer to the port you have opened.